"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   :
Case Name   : 使用多线程向表中插入uuid_short()函数;查看表中数据是否会重复
Create At   : 2023/07/15
Owner       : zou_jialiang050
Description :
        1.创建表
        2.多线程向表中插入数据
        3.查看表中数据是否重复
        4.清理环境;删除表
Expect      :
        1.创建表成功
        2.插入数据成功
        3.表中数据没有重复
        4.删除成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSQL import CommonSQL
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class UuidNotRepeat(unittest.TestCase):

    def setUp(self):
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.test_node = Node('PrimaryDbUser')
        self.comsh = CommonSH('PrimaryDbUser')
        self.comSQL = CommonSQL('PrimaryDbUser')
        self.table_name = 'test_table_011'

    def test_uuid_not_repeat(self):
        text = '------step1:创建表;expect:成功------'
        self.log.info(text)
        create_cmd = f'drop table if exists {self.table_name};' \
                     f'create table {self.table_name}(id text);'
        self.log.info(create_cmd)
        res = self.primary_sh.execut_db_sql(create_cmd)
        self.log.info(res)

        text = '------step2 多进程向表中插入数据;expect:成功------'
        self.log.info(text)
        insertcmd = f'insert into {self.table_name} values(uuid_short());'
        for i in range(200):
            insertthread = ComThread(self.primary_sh.execut_db_sql, args=(insertcmd,))
            insertthread.setDaemon(True)
            insertthread.start()
            insertthread.join(20)

        text = '------step3:查询表中是否有重复的值;expect:成功------'
        self.log.info(text)
        select_cmd = f'select * from {self.table_name} group by ' \
                     f'id having count(id)>1;'
        select_msg = self.comsh.execut_db_sql(select_cmd)
        self.log.info(select_msg)
        self.assertTrue('0 row' in select_msg.splitlines()[-1], '执行失败')
        self.log.info(select_msg)

    def tearDown(self):
        text = '------step4:清理环境;expect:成功------'
        self.log.info(text)
        drop_cmd = f'drop table {self.table_name};'
        drop_msg = self.comsh.execut_db_sql(drop_cmd)
        self.log.info(drop_msg)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')
